Convolutions with various kernels
xxxxxxxxxx5
1
begin2
large_image = load("cat_in_bowtie.jpg")3
# Since the instructor didn't include an image I've fetched one from https://pixabay.com/photos/cat-pet-pets-bowtie-blue-russian-4122355/4
image = shrink_image(large_image, 2)5
end639
959
xxxxxxxxxx1
1
size(image)21×21 OffsetArray(::Array{Float64,2}, -10:10, -10:10) with eltype Float64 with indices -10:10×-10:10:
0.000125322 0.000183256 0.000257465 … 0.000257465 0.000183256 0.000125322
0.000183256 0.000267973 0.000376488 0.000376488 0.000267973 0.000183256
0.000257465 0.000376488 0.000528946 0.000528946 0.000376488 0.000257465
0.000347542 0.000508205 0.000714002 0.000714002 0.000508205 0.000347542
0.000450738 0.000659107 0.000926011 0.000926011 0.000659107 0.000450738
0.000561654 0.000821298 0.00115388 … 0.00115388 0.000821298 0.000561654
0.000672422 0.000983272 0.00138145 0.00138145 0.000983272 0.000672422
⋮ ⋱ ⋮
0.000561654 0.000821298 0.00115388 … 0.00115388 0.000821298 0.000561654
0.000450738 0.000659107 0.000926011 0.000926011 0.000659107 0.000450738
0.000347542 0.000508205 0.000714002 0.000714002 0.000508205 0.000347542
0.000257465 0.000376488 0.000528946 0.000528946 0.000376488 0.000257465
0.000183256 0.000267973 0.000376488 0.000376488 0.000267973 0.000183256
0.000125322 0.000183256 0.000257465 … 0.000257465 0.000183256 0.000125322xxxxxxxxxx1
1
kernel = Kernel.gaussian((5, 5))xxxxxxxxxx1
1
show_colored_kernel(kernel)1.0000000000000002xxxxxxxxxx1
1
sum(kernel)xxxxxxxxxx1
1
imagexxxxxxxxxx1
1
heatmap_2d_fourier_spectrum(image)xxxxxxxxxx1
1
conv_image = convolve(image, kernel)xxxxxxxxxx1
1
heatmap_2d_fourier_spectrum(conv_image)3×3 OffsetArray(::Array{Float64,2}, -1:1, -1:1) with eltype Float64 with indices -1:1×-1:1:
-0.5 -1.0 -0.5
-1.0 7.0 -1.0
-0.5 -1.0 -0.5xxxxxxxxxx4
1
K = centered(2
[ -0.5 -1.0 -0.53
-1.0 7.0 -1.04
-0.5 -1.0 -0.5 ])xxxxxxxxxx1
1
show_colored_kernel(K)xxxxxxxxxx1
1
conv_image_2 = convolve(image, K)3×3 OffsetArray(::Array{Float64,2}, -1:1, -1:1) with eltype Float64 with indices -1:1×-1:1:
-0.125 -0.25 -0.125
0.0 0.0 0.0
0.125 0.25 0.125xxxxxxxxxx1
1
edge_kernel = Kernel.sobel()[1]xxxxxxxxxx1
1
show_colored_kernel(edge_kernel)xxxxxxxxxx1
1
conv_image_3 = Gray.(abs.(convolve(image, edge_kernel)))xxxxxxxxxx1
1
imagexxxxxxxxxx1
1
plot_1d_fourier_spectrum(image)xxxxxxxxxx4
1
img = mktemp() do fn,f2
download("https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Zebra_Herd_Michael_makalundwa.jpg/1280px-Zebra_Herd_Michael_makalundwa.jpg", fn)3
load(fn)4
endxxxxxxxxxx1
1
plot_1d_fourier_spectrum(image)xxxxxxxxxx1
1
# Not great, let's crop it...853
1280
xxxxxxxxxx1
1
size(img)xxxxxxxxxx1
1
img[280:end-100, :]xxxxxxxxxx1
1
plot_1d_fourier_spectrum(img[280:end-100, :])xxxxxxxxxx1
1
heatmap_2d_fourier_spectrum(img[280:end-100, :])xxxxxxxxxx1
1
heatmap_2d_fourier_spectrum(convolve(img[280:end-100, :], kernel))xxxxxxxxxx1
1
Gray.(abs.(convolve(convert.(ColorTypes.RGB{Float64}, img[280:end-100, :]), Kernel.sobel()[2])))Function definitions
xxxxxxxxxx3
1
md"""2
# Function definitions3
"""show_colored_kernel (generic function with 1 method)decimate (generic function with 2 methods)shrink_image (generic function with 2 methods)rgb_to_float (generic function with 1 method)fourier_spectrum_magnitudes (generic function with 1 method)plot_1d_fourier_spectrum (generic function with 2 methods)heatmap_2d_fourier_spectrum (generic function with 1 method)clamp_at_boundary (generic function with 1 method)rolloff_boundary (generic function with 1 method)convolve (generic function with 2 methods)box_blur (generic function with 1 method)gauss_blur (generic function with 2 methods)